﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using Granite.Xaml;
using Toon.Models.Characters;
using Microsoft.Win32;
using System.IO;
using System.Windows;
using System.Xml.Linq;
using System.Windows.Navigation;
using Toon.Views;

namespace Toon
{
	public class PrimaryViewModel : NavigationViewModel
	{
		/// <summary>
		/// Initializes a new instance of the PrimaryViewModel class.
		/// </summary>
		public PrimaryViewModel(NavigationWindow navigator)
			: base(navigator)
		{ }

		public ICommand OpenToonWebsiteCommand
		{
			get { return GetCommand(OpenToonWebsite, "OpenToonWebsiteCommand"); }
		}

		public void OpenToonWebsite()
		{
			OpenWebsite(@"http://www.sjgames.com/toon/");
		}

		public Character CurrentCharacter
		{
			get { return Get<Character>("CurrentCharacter"); }
			set { Set(value, "CurrentCharacter"); }
		}

		public RuleSet CurrentRuleSet
		{
			get { return Get<RuleSet>("CurrentRuleSet"); }
			set { Set(value, "CurrentRuleSet"); }
		}

		public ICommand CreateNewCommand
		{
			get { return GetCommand(CreateNew, "CreateNewCommand"); }
		}

		private void CreateNew()
		{
			CurrentCharacter = CurrentRuleSet.CreateNewCharacter();
			Navigate(typeof(NewCharacter));
		}

		public ICommand CreateRandomCommand
		{
			get { return GetCommand(CreateRandom, "CreateRandomCommand"); }
		}

		private void CreateRandom()
		{
			CurrentCharacter = CurrentRuleSet.CreateRandomCharacter();
			Navigate(typeof(NewCharacter));
		}

		public ICommand SaveCommand
		{
			get { return GetCommand(Save, "SaveCommand"); }

		}

		public void Save()
		{
			var xml = CurrentCharacter.ToXml();

			var dlg = new SaveFileDialog();
			dlg.DefaultExt = ".toon-character";
			dlg.Filter = "Toon Character (*.toon-character)|*.toon-character|Text File (*.txt)|*.txt";
			dlg.FileName = CurrentCharacter.GetSuggestedFileName();

			if (dlg.ShowDialog() == true)
			{
				CurrentCharacter.FileName = dlg.SafeFileName;
				using (var stream = new StreamWriter(dlg.OpenFile()))
				{
					if (dlg.FilterIndex == 1)
						stream.Write(xml);
					if (dlg.FilterIndex == 2)
						stream.Write("Hi, I'm a text file!");

					CurrentCharacter.AcceptChanges();
				}
			}
		}

		public ICommand OpenCommand
		{
			get { return GetCommand(Open, "OpenCommand"); }

		}

		public void Open()
		{
			try
			{
				var dlg = new OpenFileDialog();
				dlg.Filter = "Toon Characters (*.toon-character)|*.toon-character|All Files (*.*)|*.*";
				if (dlg.ShowDialog() == true)
				{
					using (var stream = new StreamReader(dlg.OpenFile()))
					{
						var xml = stream.ReadToEnd();
						CurrentCharacter = new Character(XElement.Parse(xml));
						CurrentCharacter.FileName = dlg.SafeFileName;
					}
				}
			}
			catch
			{
				MessageBox.Show("Unable to open file");
				return;
			}

			Navigate(typeof(EditCharacter));
		}

		public ICommand PrintCommand
		{
			get { return GetCommand(Print, "PrintCommand"); }

		}

		public void Print()
		{
			//var doc = new PrintDocument();
			//doc.PrintPage += doc_PrintPage;
			//doc.Print(CurrentCharacter.Name);
		}

		//void doc_PrintPage(object sender, PrintPageEventArgs e)
		//{
		//	var newCharacterSheet = new CharacterSheet();

		//	newCharacterSheet.EditMode = EditMode.Printable;
		//	newCharacterSheet.Width = e.PrintableArea.Width;
		//	newCharacterSheet.Height = e.PrintableArea.Height;

		//	newCharacterSheet.DataContext = this;
		//	e.PageVisual = newCharacterSheet;
		//}

		public ICommand RestartCommand
		{
			get { return GetCommand(Restart, "RestartCommand"); }
		}

		public void Restart()
		{
			Navigate(typeof(Home));
		}
	}
}
